//
//  YHMeAccountViewController.m
//  YHApp
//
//  Created by -- on 2021/7/23.
//

#import "HAAccountVC.h"
#import "HAAddressPopView.h"
#import "HAAccountInfoMo.h"
#import "HABankMo.h"
#import "HAChooseTabPopView.h"

@interface HAAccountVC ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArray;

/***/
@property (nonatomic, strong) HAAccountInfoMo *infoModel;
/***/
@property (nonatomic, strong) HABankMo *selBankModel;

@property (nonatomic, strong) UITextField *mobileTextField;
/***/
@property (nonatomic, strong) UITextField *bankZhiHangTextField;
/***/
@property (nonatomic, strong) UITextField *bankNumTextField;

@property (nonatomic, strong) UITextField *surnameTextField;
/***/
@property (nonatomic, strong) UITextField *alipayTextField;
/***/
@property (nonatomic, strong) UITextField *alipayNameTextField;

@property (nonatomic, strong) UITextField *identityTextField;


/***/
@property (nonatomic, strong) NSString *region;

@property (nonatomic, strong) NSString *mobile;

/***/
@property (nonatomic, strong) NSString *identity;

/***/
@property (nonatomic, strong) NSString *branch;
/***/
@property (nonatomic, strong) NSString *bank_account;
/***/
@property (nonatomic, strong) NSString *alipay_no;
/***/
@property (nonatomic, strong) NSString *alipay_account;
/***/
@property (nonatomic, strong) NSString *surname;

@end

@implementation HAAccountVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.baseTLabel.text = @"结算账户";

    [self loadData];

    // Do any additional setup after loading the view.
}

- (void)loadData {
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"account/acc";
    [MBProgressHUD showLoading:self.view];
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            [MBProgressHUD hideHUDForView:self.view];
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                self.infoModel = [HAAccountInfoMo mj_objectWithKeyValues:dataModel.data];
                //默认选中已有的
                HABankMo *selBankModel = [[HABankMo alloc]init];
                selBankModel.name = self.infoModel.bank_name;
                selBankModel.ID = self.infoModel.bank_id;
                self.selBankModel = selBankModel;
                
                self.region = self.infoModel.region;
                self.branch = self.infoModel.branch;
                self.surname = self.infoModel.surname;
                self.identity = self.infoModel.identity;

                self.bank_account = self.infoModel.bank_account;
                self.mobile = self.infoModel.mobile;

                self.alipay_no = self.infoModel.alipay_no;
                self.alipay_account = self.infoModel.alipay_account;

                [self initUI];
                
//                [self.dataArray addObject:@"个人信息"];
//                [self.dataArray addObject:@"代理名称"];
//                [self.dataArray addObject:@"身份证号"];
                
                [self.dataArray addObject:@"银联账户"];
                [self.dataArray addObject:@"收款银行"];
                [self.dataArray addObject:@"省市区/县"];
                [self.dataArray addObject:@"收款支行"];
                [self.dataArray addObject:@"结算法人"];
                [self.dataArray addObject:@"身份证号"];
                [self.dataArray addObject:@"收款卡号"];
                [self.dataArray addObject:@"预留手机号"];

                [self.dataArray addObject:@"支付宝"];
                [self.dataArray addObject:@"收款人名称"];
                [self.dataArray addObject:@"支付宝账户"];

                [self.dataArray addObject:@"提交"];

                [self.tableView reloadData];
            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];
}




-(void)mitButtonClick {
    [self.view endEditing:YES];

    NSDictionary *dict = @{
        @"mobile":self.mobile?:@"",
        @"bank_id":self.selBankModel.ID?:@"",
        @"bank_account":self.bank_account?:@"",
        @"region":self.region?:@"",
        @"branch":self.branch?:@"",
        @"alipay_no":self.alipay_no?:@"",
        @"identity":self.identity?:@"",
        @"surname":self.surname?:@"",
        @"alipay_account":self.alipay_account?:@"",
    };

    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodPOST;
    request.requestUrl = @"account/settle";
    request.parms = dict;
    [MBProgressHUD showLoading:self.view];
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            [MBProgressHUD hideHUDForView:self.view];
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                [MBProgressHUD showSuccess:@"提交成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];
}

- (void)initUI{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(app_topH+44);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"提交"]) {
        return 54+60;
    }
    return 54;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHMeAccountViewCellID"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHMeAccountViewCellID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if ([title isEqualToString:@"个人信息"]) {
        [self setCellTit:cell.contentView title:title];
    }else if ([title isEqualToString:@"代理名称"]) {
        [self setCellContent:cell.contentView title:title content:self.infoModel.surnamePreview isLine:YES endEditing:NO];
    }else if ([title isEqualToString:@"身份证号"]) {
        [self setCellContent:cell.contentView title:title content:self.identity isLine:NO endEditing:YES];
    }else if ([title isEqualToString:@"银联账户"]) {
        [self setCellTit:cell.contentView title:title];
    }else if ([title isEqualToString:@"结算法人"]) {
        [self setCellContent:cell.contentView title:title content:self.surname isLine:YES endEditing:YES];
    }else if ([title isEqualToString:@"收款银行"]) {
        NSString *content = @"";
        if (self.selBankModel) {
            content = self.selBankModel.name;
        }else {
            content = @"请选择开户银行";
        }
        [self setCellContent:cell.contentView title:title content:content isLine:YES endEditing:NO];
    }else if ([title isEqualToString:@"省市区/县"]) {
        NSString *content = @"";
        if (![NSString isEmptyString:self.region]) {
            content = self.region;
        }else {
            content = @"请选择省市区/县";
        }
        [self setCellContent:cell.contentView title:title content:content isLine:YES endEditing:NO];
    }else if ([title isEqualToString:@"收款支行"]) {
        [self setCellContent:cell.contentView title:title content:self.branch isLine:YES endEditing:YES];
    }else if ([title isEqualToString:@"收款卡号"]) {
        [self setCellContent:cell.contentView title:title content:self.bank_account isLine:YES endEditing:YES];
    }else if ([title isEqualToString:@"支付宝"]) {
        [self setCellTit:cell.contentView title:@"支付宝账户"];
    }else if ([title isEqualToString:@"收款人名称"]) {
        [self setCellContent:cell.contentView title:title content:self.alipay_account isLine:YES endEditing:YES];
    }else if ([title isEqualToString:@"支付宝账户"]) {
        [self setCellContent:cell.contentView title:title content:self.alipay_no isLine:NO endEditing:YES];
    }else if ([title isEqualToString:@"提交"]) {
        [self setCellMit:cell.contentView];
    }else if ([title isEqualToString:@"预留手机号"]) {
        [self setCellContent:cell.contentView title:title content:self.mobile isLine:NO endEditing:YES];
    }
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"省市区/县"]) {
        [self laodAddress];
    }else if ([title isEqualToString:@"收款银行"]) {
        [self laodBanks];
    }
}


-(void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == self.bankZhiHangTextField) {
        self.branch = textField.text;
    }
    if (textField == self.bankNumTextField) {
        self.bank_account = textField.text;
    }
    if (textField == self.alipayTextField) {
        self.alipay_no = textField.text;
    }
    if (textField == self.alipayNameTextField) {
        self.alipay_account = textField.text;
    }
    if (textField == self.surnameTextField) {
        self.surname = textField.text;
    }
    if (textField == self.mobileTextField) {
        self.mobile = textField.text;
    }
    if (textField == self.identityTextField) {
        self.identity = textField.text;
    }

}


-(void)setCellContent:(UIView *)contentView title:(NSString *)title content:(NSString *)content isLine:(BOOL)isLine endEditing:(BOOL)endEditing {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    UILabel *titLabel = [UILabel ha_NLabelWithFont:17 textColor:@"#000000" text:title alignment:0];
    [contentView addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.width.mas_offset(88);
    }];
    
    UITextField *textField = [[UITextField alloc]init];
    textField.delegate = self;
    textField.textColor = [UIColor colorWithHexString:@"#121B32"];
    textField.font = [UIFont systemFontOfSize:16];
    textField.textAlignment = 2;
    [contentView addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-20);
        make.left.mas_equalTo(titLabel.mas_right);
        make.height.mas_offset(32);
    }];
    
    textField.enabled = endEditing;
    textField.text = content;

    if ([title isEqualToString:@"代理名称"]) {
        textField.placeholder = @"请输入代理名称";
//        textField.textColor = [UIColor colorWithHexString:@"#898b5c"];
    }
    if ([title isEqualToString:@"身份证号"]) {
        self.identityTextField = textField;
        textField.placeholder = @"请输入身份证号";
//        textField.textColor = [UIColor colorWithHexString:@"#898b5c"];
    }
    if ([title isEqualToString:@"结算法人"]) {
        self.surnameTextField = textField;
        textField.placeholder = @"请输入结算法人";
        textField.enabled = [NSString isEmptyString:self.surname];
    }
    if ([title isEqualToString:@"收款支行"]) {
        self.bankZhiHangTextField = textField;
        textField.placeholder = @"请输入支行";
    }
    if ([title isEqualToString:@"收款卡号"]) {
        self.bankNumTextField = textField;
        textField.placeholder = @"请输入银行卡号";
    }
    if ([title isEqualToString:@"支付宝账户"]) {
        self.alipayTextField = textField;
        textField.placeholder = @"请输入支付宝账户";
    }
    if ([title isEqualToString:@"收款人名称"]) {
        self.alipayNameTextField= textField;
        textField.placeholder = @"收款人名称";
        textField.enabled = [NSString isEmptyString:self.alipay_account];
    }
    
    if ([title isEqualToString:@"预留手机号"]) {
        self.mobileTextField = textField;
        textField.placeholder = @"请输入预留手机号";
    }

    if (isLine == YES) {
        UIView *lineView = [UIView ha_createView];
        lineView.backgroundColor = [UIColor kLineColor];
        [contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(contentView).offset(20);
            make.right.equalTo(contentView).offset(-20);
            make.height.equalTo(@0.6);
            make.bottom.equalTo(contentView);
        }];
    }
}

-(void)setCellMit:(UIView *)contentView {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    contentView.backgroundColor = [UIColor khaBGColor];
    
    UIButton *mitButton = [[UIButton alloc]init];
    [mitButton setTitle:@"提交" forState:UIControlStateNormal];
    [mitButton setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
    mitButton.titleLabel.font = [UIFont systemFontOfSize:20];
    mitButton.backgroundColor = [UIColor colorWithHexString:@"#19191A"];
    [mitButton addTarget:self action:@selector(mitButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:mitButton];
    [mitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(25);
        make.right.mas_equalTo(-25);
        make.centerY.mas_equalTo(0);
        make.height.mas_offset(54);
    }];
}

-(void)setCellTit:(UIView *)contentView title:(NSString *)title {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    contentView.backgroundColor = [UIColor khaBGColor];
    
    UILabel *titLabel = [UILabel ha_NLabelWithFont:15 textColor:@"#898b5c" text:title alignment:0];
    [contentView addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(0);
    }];
}


- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // 设置背景颜色
        _tableView.backgroundColor = [UIColor khaBGColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array ];
    }
    return _dataArray;
}


-(void)laodAddress {
    WS(weakSelf)
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"region/lists.html";
    [MBProgressHUD showLoading:self.view];
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            [MBProgressHUD hideHUDForView:self.view];
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                NSArray *arr = [HAAddressProvinceMo mj_objectArrayWithKeyValuesArray:dataModel.data];
                
                HAAddressPopView *menu = [[HAAddressPopView alloc]initWithDZArray:arr];
                [menu show];
                menu.HAAddressPopViewSureBlock = ^(NSString * _Nonnull province, NSString * _Nonnull city, NSString * _Nonnull area) {
                    weakSelf.region = [NSString stringWithFormat:@"%@ %@ %@",province,city,area];
                    [weakSelf.tableView reloadData];
                };
            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];
}

-(void)laodBanks {
    WS(weakSelf)
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"index/banks";
    [MBProgressHUD showLoading:self.view];
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            [MBProgressHUD hideHUDForView:self.view];
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                NSArray *arr = [HABankMo mj_objectArrayWithKeyValuesArray:dataModel.data];
                NSMutableArray *dicts = [NSMutableArray array];
                for (HABankMo *mo in arr) {
                    [dicts addObject:@{@"color":@"#373737",@"title":mo.name?:@""}];
                }
                HAChooseTabPopView *menu = [[HAChooseTabPopView alloc]initWithDicts:dicts title:@"选择开户银行"];
                [menu show];
                menu.YHChooseTabMenuCellBlock = ^(NSString * _Nonnull title) {
                    for (HABankMo *mo in arr) {
                        if ([title isEqualToString:mo.name]) {
                            weakSelf.selBankModel = mo;
                            break;
                        }
                    }
                    [weakSelf.tableView reloadData];
                };
            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];
}




@end
